package com.ziyun.erp.modules.quoteProductAttributeOptionRelation.service.impl;

import com.ziyun.erp.modules.capital.entity.MerchantApplyCashEntity;
import com.ziyun.erp.modules.quoteProductAttributeOptionRelation.vo.QuoteAttributeSpecialVo;
import com.ziyun.erp.modules.quoteProductAttributeOptionRelation.vo.QuoteOptionSpecialVo;
import io.renren.common.exception.RRException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.ziyun.erp.modules.quoteProductAttributeOptionRelation.dao.QuoteProductAttributeOptionRelationDao;
import com.ziyun.erp.modules.quoteProductAttributeOptionRelation.entity.QuoteProductAttributeOptionRelationEntity;
import com.ziyun.erp.modules.quoteProductAttributeOptionRelation.service.QuoteProductAttributeOptionRelationService;


@Service("quoteProductAttributeOptionRelationService")
public class QuoteProductAttributeOptionRelationServiceImpl extends ServiceImpl<QuoteProductAttributeOptionRelationDao, QuoteProductAttributeOptionRelationEntity> implements QuoteProductAttributeOptionRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<QuoteProductAttributeOptionRelationEntity> page = this.selectPage(
                new Query<QuoteProductAttributeOptionRelationEntity>(params).getPage(),
                new EntityWrapper<QuoteProductAttributeOptionRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryAttributeSpecialPage(Map<String, Object> params) {

        Page<QuoteAttributeSpecialVo> page = new Query<QuoteAttributeSpecialVo>(params).getPage();

        page.setRecords(this.baseMapper.selectAttributeSpecialList(page, params));

        return new PageUtils(page);
    }

    @Override
    public List<Map<String, Object>> getAttributes(Map<String, Object> paramsMap) {
        return this.baseMapper.getAttributes(paramsMap);
    }

    @Override
    public void saveOrUpdateAttributeSpecial(QuoteAttributeSpecialVo quoteAttributeSpecialVo) {
        QuoteProductAttributeOptionRelationEntity quoteProductAttributeOptionRelation = this.attributeSpecialVoToEntity(quoteAttributeSpecialVo);
        if (quoteAttributeSpecialVo.getId() == null){ // 新增
            // 校验重复
           int count = this.selectCount(new EntityWrapper<QuoteProductAttributeOptionRelationEntity>()
                    .eq("category_id", quoteAttributeSpecialVo.getCategoryId())
                    .eq("attribute_id", quoteAttributeSpecialVo.getAttributeId())
                    .eq(quoteAttributeSpecialVo.getPartsId() !=null,"parts_id",quoteAttributeSpecialVo.getPartsId()));
           if (count >0){
               throw new RRException("当前属性已经存在互斥,请返回列表点击编辑!");
           }
           this.insert(quoteProductAttributeOptionRelation);

        }else { //修改
            this.updateById(quoteProductAttributeOptionRelation);
        }
    }

    /**
     * 根据id 获取属性特规详情
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> getAttributeSpecialInfo(Integer id) {
        Map<String,Object> rs = new HashMap<>();
        QuoteAttributeSpecialVo quoteAttributeSpecialVo = this.baseMapper.selectAttributeSpecialById(id);
        //查询当前部件 或者分类下的属性
        Map<String ,Object> paramsMap = new HashMap<>();
        paramsMap.put("categoryId",quoteAttributeSpecialVo.getCategoryId());
        paramsMap.put("partsId",quoteAttributeSpecialVo.getPartsId());
        List<Map<String,Object>> attributeList = this.baseMapper.getAttributes(paramsMap);
        rs.put("attributeList",attributeList);
        rs.put("quoteAttributeSpecial",quoteAttributeSpecialVo);
        return rs;
    }

    @Override
    public PageUtils queryOptionSpecialPage(Map<String, Object> params) {
        Page<QuoteOptionSpecialVo> page = new Query<QuoteOptionSpecialVo>(params).getPage();

        page.setRecords(this.baseMapper.selectOptionSpecialList(page, params));

        return new PageUtils(page);
    }

    @Override
    public List<Map<String, Object>> getOptions(Map<String, Object> paramsMap) {
        return this.baseMapper.getOptions(paramsMap);
    }

    @Override
    public void saveOrUpdateOptionSpecial(QuoteOptionSpecialVo optionSpecialVo) {
        QuoteProductAttributeOptionRelationEntity quoteProductAttributeOptionRelation = this.optionSpecialVoToEntity(optionSpecialVo);
        if (optionSpecialVo.getId() == null){ // 新增
            // 校验重复
            int count = this.selectCount(new EntityWrapper<QuoteProductAttributeOptionRelationEntity>()
                    .eq("category_id", optionSpecialVo.getCategoryId())
                    .eq("option_id", optionSpecialVo.getOptionId())
                    .eq(optionSpecialVo.getPartsId() !=null,"parts_id",optionSpecialVo.getPartsId()));
            if (count >0){
                throw new RRException("当前选项已经存在互斥,请返回列表点击编辑!");
            }
            this.insert(quoteProductAttributeOptionRelation);

        }else { //修改
            this.updateById(quoteProductAttributeOptionRelation);
        }
    }

    @Override
    public Map<String, Object> getOptionSpecialInfo(Integer id) {
        Map<String,Object> rs = new HashMap<>();
        QuoteOptionSpecialVo quoteOptionSpecialVo = this.baseMapper.selectOptionSpecialById(id);
        //查询当前部件 或者分类下的属性
        Map<String ,Object> paramsMap = new HashMap<>();
        paramsMap.put("categoryId",quoteOptionSpecialVo.getCategoryId());
        paramsMap.put("partsId",quoteOptionSpecialVo.getPartsId());
        List<Map<String,Object>> attributeList = this.baseMapper.getAttributes(paramsMap);
        // 查询当前分类/部件下 所有属性选项
        paramsMap.remove("attributeId");
        List<Map<String,Object>> optionAllList = this.getOptions(paramsMap);
        rs.put("attributeList",attributeList);
        rs.put("optionAllList",optionAllList);
        rs.put("quoteOptionSpecial",quoteOptionSpecialVo);

        return rs;
    }


    //属性特规 vo 转换成 Entity
    private QuoteProductAttributeOptionRelationEntity attributeSpecialVoToEntity (QuoteAttributeSpecialVo quoteAttributeSpecialVo){
        QuoteProductAttributeOptionRelationEntity quoteProductAttributeOptionRelation = new QuoteProductAttributeOptionRelationEntity();
        quoteProductAttributeOptionRelation.setId(quoteAttributeSpecialVo.getId());
        quoteProductAttributeOptionRelation.setCategoryId(quoteAttributeSpecialVo.getCategoryId());
        quoteProductAttributeOptionRelation.setPartsId(quoteAttributeSpecialVo.getPartsId());
        quoteProductAttributeOptionRelation.setAttributeId(quoteAttributeSpecialVo.getAttributeId());
        quoteProductAttributeOptionRelation.setAttributeDefinitionLength(quoteAttributeSpecialVo.getAttributeDefinitionLength());
        quoteProductAttributeOptionRelation.setAttributeMustChoice(quoteAttributeSpecialVo.getAttributeMustChoice());
        quoteProductAttributeOptionRelation.setAttributeIgnoreDefinition(quoteAttributeSpecialVo.getAttributeIgnoreDefinition());
        quoteProductAttributeOptionRelation.setAttributeDisplay(quoteAttributeSpecialVo.getAttributeDisplay());
        quoteProductAttributeOptionRelation.setAttributeAttributeOutIds(quoteAttributeSpecialVo.getAttributeAttributeOutIds());
        return quoteProductAttributeOptionRelation;
    }

    //  选项特规 vo 转换成 Entity
    private QuoteProductAttributeOptionRelationEntity optionSpecialVoToEntity (QuoteOptionSpecialVo quoteOptionSpecialVo){
        QuoteProductAttributeOptionRelationEntity quoteProductAttributeOptionRelation = new QuoteProductAttributeOptionRelationEntity();
        quoteProductAttributeOptionRelation.setId(quoteOptionSpecialVo.getId());
        quoteProductAttributeOptionRelation.setCategoryId(quoteOptionSpecialVo.getCategoryId());
        quoteProductAttributeOptionRelation.setPartsId(quoteOptionSpecialVo.getPartsId());
        quoteProductAttributeOptionRelation.setOptionId(quoteOptionSpecialVo.getOptionId());
        quoteProductAttributeOptionRelation.setOptionDefinitionLength(quoteOptionSpecialVo.getOptionDefinitionLength());
        quoteProductAttributeOptionRelation.setOptionIgnoreDefinition(quoteOptionSpecialVo.getOptionIgnoreDefinition());
        quoteProductAttributeOptionRelation.setOptionMustChoice(quoteOptionSpecialVo.getOptionMustChoice());
        quoteProductAttributeOptionRelation.setOptionAttributeOutIds(quoteOptionSpecialVo.getOptionAttributeOutIds());
        quoteProductAttributeOptionRelation.setOptionOptionOutIds(quoteOptionSpecialVo.getOptionOptionOutIds());
        return quoteProductAttributeOptionRelation;
    }

}
